package com.voute.Utils;


import com.voute.Base.BaseFragment;

import java.util.HashMap;

/**
 * Fragment工具类
 * Created by liukanshan on 2016/12/9.
 */

public class ViewUtils {

    private static HashMap<String, BaseFragment> fragments = new HashMap<>();

    public static BaseFragment createFragment(Class<?> clazz, boolean is_add) {
        if (is_add) {
            if (fragments.get(clazz.getName()) == null) {
                try {
                    BaseFragment fragment = (BaseFragment) clazz.forName(clazz.getName()).newInstance();
                    fragments.put(clazz.getName(), fragment);
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            return fragments.get(clazz.getName());
        } else {
            BaseFragment fragment = null;
            try {
                fragment = (BaseFragment) clazz.forName(clazz.getName()).newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return fragment;
        }
    }
}
